package com.eeepay.eeepay_v2.activity;

import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.TitleBar;
import com.google.protobuf.Type;

/**
 * Created by zw on 2016/3/14 0014.
 * 人员管理
 */
public class PersonManageActivity extends ABBaseActivity implements View.OnClickListener {

    private TitleBar titleBar;
    private TextView[] role_select = new TextView[2];//角色2种
    private TextView[] state_select = new TextView[2];//用户状态2种
    private LabelEditText et_phone;
    private Button btn_confirm;//查询

    private String role = "";
    private String state = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person_manage;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setRightTextView("添加");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        role_select[0] = getViewById(R.id.tv_select_1);
        role_select[1] = getViewById(R.id.tv_select_2);
        state_select[0] = getViewById(R.id.tv_status_select_1);
        state_select[1] = getViewById(R.id.tv_status_select_2);

        et_phone = getViewById(R.id.et_phone);
        et_phone.getEditText().setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        et_phone.getEditText().setInputType(Type.FIELDS_FIELD_NUMBER);
        btn_confirm = getViewById(R.id.btn_confirm);
    }

    @Override
    protected void eventOnClick() {
        //添加
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                goActivity(PersonAddActivity.class);
            }
        });

        role_select[0].setOnClickListener(this);
        role_select[1].setOnClickListener(this);
        state_select[0].setOnClickListener(this);
        state_select[1].setOnClickListener(this);

        btn_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_confirm:
                queryResult();
                break;
            case R.id.tv_select_1:
                changeRoleSelected(0);
                break;
            case R.id.tv_select_2:
                changeRoleSelected(1);
                break;
            case R.id.tv_status_select_1:
                changeSelected(0);
                break;
            case R.id.tv_status_select_2:
                changeSelected(1);
                break;
        }
    }

    private void queryResult() {

        Bundle bundle = new Bundle();
        bundle.putString("role", role);
        bundle.putString("state", state);
        bundle.putString("phone", et_phone.getEditText().getText().toString());
        goActivity(PersonListActivity.class, bundle);
    }

    /**
     * 改变选择框的选中状态
     * @param num
     */
    private void changeSelected(int num) {
        for(int i = 0; i < state_select.length; i++){
            if(i == num){
                if(state_select[i].isSelected()){
                    state_select[i].setSelected(false);
                    state = "";
                }else{
                    state_select[i].setSelected(true);
                    if(i == 0){
                        state = "1";
                    }else if(i == 1){
                        state = "0";
                    }
                }
            }else{
                state_select[i].setSelected(false);
            }
        }
    }

    /**
     * 改变选择框的选中状态
     * @param num
     */
    private void changeRoleSelected(int num) {
        for(int i = 0; i < role_select.length; i++){
            if(i == num){
                if(role_select[i].isSelected()){
                    role_select[i].setSelected(false);
                    role = "";
                }else{
                    role_select[i].setSelected(true);
                    if(i == 0){
                        role = "1";
                    }else if(i == 1){
                        role = "0";
                    }
                }
            }else{
                role_select[i].setSelected(false);
            }
        }
    }
}
